package com.fmatos.amazingwallpapers.imagemanagement;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;

import com.fmatos.amazingwallpapers.image.database.ImagesDataSource;
import com.fmatos.amazingwallpapers.imagemanagement.BitmapsRetrieverManager.ImageView;
import com.fmatos.amazingwallpapers.preferences.AmazingPreferences;
import com.fmatos.amazingwallpapers.wallpaper.domain.AbstractAmazingWallpaper;

// TODO make inner class that has canvas as a field, and stop passing as parameters everywhere
public class WallpaperBitmaps extends AbstractAmazingWallpaper {

	private static final String TAG = WallpaperBitmaps.class.getSimpleName();
	private BitmapsRetrieverManager bitmapsManager;
	private AmazingPreferences amazingPreferences;


	public WallpaperBitmaps(Context context) {


		amazingPreferences = new AmazingPreferences(context);		
		bitmapsManager = new BitmapsRetrieverManager(context);
	
	}
	
	@Override
	protected void concreteDraw(Canvas canvas) {
		
		drawImage(canvas);
		
	}

	private void drawImage(Canvas canvas) {

		ImageView view = bitmapsManager.getNextImage(); 

		if ( view != null && view.bitmap != null && isVisible() ) {
			Log.d(TAG,"image width " + view.bitmap.getWidth() + " height = " + view.bitmap.getHeight());
			clearScreen(canvas);
			
			
			drawFullScreenBitmap(canvas,view.bitmap);
			drawText(canvas,view.caption);
			
		}
	}
	
	private void drawFullScreenBitmap(Canvas canvas, Bitmap bitmap ) {
		float scale = computeScale(canvas,bitmap);
		Matrix matrix = new Matrix();
		
		canvas.save();
		
		matrix.postTranslate(canvas.getWidth()/2, canvas.getHeight()/2); // put image left corner in center screen
		matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); // put image center in center screen
		matrix.postScale(scale, scale, canvas.getWidth()/2, canvas.getHeight()/2); // scale around center of screen
				
		canvas.setMatrix(matrix);
		canvas.drawBitmap(bitmap, 0, 0, null);

		canvas.restore();
		
	}

	private float computeScale(Canvas canvas, Bitmap bitmap) {

		float zX = (float)canvas.getWidth() /(float) bitmap.getWidth();
		float zY = (float)canvas.getHeight() / (float)bitmap.getHeight();
		
		return Math.min(zX, zY);
	}

	private void clearScreen(Canvas canvas) {
		Paint p = new Paint();
		p.setColor(Color.BLACK);
		canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), p);
	}

	// TODO check if text is too large for screen and break it in 2 lines https://gist.github.com/markusfisch/2655909
	// TODO measure status bar and push things lower
	private void drawText(Canvas canvas,String text)	{
		
		int fontSize = getFontSize(canvas);
		Paint p = new Paint();
		
		
		p.setTextSize(fontSize); // TODO make font larger for S4  
		p.setAntiAlias(true);
		int shadeSize = fontSize/15;

		float w = p.measureText(text, 0, text.length());
		if ( w > canvas.getWidth() ) {
			Log.w(TAG,"Text is too wide for screen");
		}
		
		int offset = (int) w / 2;
		int x = canvas.getWidth()/2 - offset;
		int y = amazingPreferences.getStatusBarHeight() + fontSize ; 

		p.setColor(Color.BLACK);
		
		canvas.drawText(text, x + shadeSize, y + shadeSize, p);
		p.setColor(Color.WHITE);
		canvas.drawText(text, x, y, p);
	}

	/**
	 * computes font size in pixels
	 * @return
	 */
	private int getFontSize(Canvas canvas) {

		float spiTextSize = 20.f; // I guess this is the font size in pixels
		
//		amazingPreferences.getScaledDensity() 
		float sz = (float)spiTextSize * (float)amazingPreferences.getDpi() / 160f;
		
		Log.d(TAG,"Font size in pixels is " + sz);
		return (int) sz;
	}



}
